package net.admin.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.admin.db.item.CategoryDAO;
import net.admin.db.item.ItemDAO;
import net.admin.action.Action;
import net.admin.action.ActionForward;

public class ItemRegistAction implements Action {
	         
	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ActionForward af = new ActionForward();
		af.setRedirect(false);
		boolean result=false;
		String item_cd = null;
		ItemDAO dao = null;
		af.setPath("/admin/item/item_reg.jsp");
		
		HttpSession session = request.getSession();
		String admin_id=(String)session.getAttribute("user_id");
		String item_nm = request.getParameter("item_nm");
		String category_nm = request.getParameter("category_nm");
		
		try{
			dao = new ItemDAO();
			String ct_cd=dao.getCategoryCode(category_nm);
			if(ct_cd!=null){
				request.setAttribute("category_cd", null);
				item_cd=dao.registItem(admin_id, item_nm, ct_cd);
			}
			request.setAttribute("item_cd", item_cd);
			request.setAttribute("item_nm", item_nm);
			request.setAttribute("category_cd", ct_cd);
			request.setAttribute("category_nm", category_nm);
			request.setAttribute("result", item_cd!=null?"Y":"N");
		}catch(Exception e){
			request.setAttribute("result", "N");
			e.printStackTrace();
		}finally{
			dao.close();
		}
		
		return af;
	}

}
